package br.edu.ifrn.utineo.web;

import br.edu.ifrn.utineo.jdbc.Conexao;
import br.edu.ifrn.utineo.modelo.Pacientes;
import br.edu.ifrn.utineo.modelo.Sistema;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.*;
import javax.servlet.RequestDispatcher;


/**
 * Servlet implementation class CadastrarPacienteServlet
 */
public class CadastrarPacienteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CadastrarPacienteServlet() {
        
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Pacientes p = new Pacientes();
            Sistema sis = Sistema.getInstance();

            String dataNasc = request.getParameter("DataNascimento");
            String dataInter = request.getParameter("DataInternacao");

            DateFormat fmt = new SimpleDateFormat("dd/MM/yy");
        try {
            java.sql.Date data = new java.sql.Date(fmt.parse(dataNasc).getTime());
            p.setDataNascimento(data);

            data = new java.sql.Date(fmt.parse(dataNasc).getTime());
            p.setDataInternacao(data);

             p.setRegistro( Integer.parseInt(request.getParameter("Registro")));
             p.setNomeMae(request.getParameter("NomeMae"));
             p.setSexo(request.getParameter("Sexo"));
             p.setLeito(Integer.parseInt(request.getParameter("Leito")));
             p.setPesoAlta(Float.parseFloat(request.getParameter("PesoAlta")));
             p.setPesoNascimento(Float.parseFloat(request.getParameter("PesoNascimento")));
             p.setCRM(Integer.parseInt(request.getParameter("ID")));
            
        } catch (ParseException ex) {
            Logger.getLogger(CadastrarPacienteServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
         
        try {
            Conexao cnx = new Conexao();
            cnx.adiciona(p);

            int x = p.getCRM();
            request.setAttribute("medico", sis.getFuncionario(x) );
            RequestDispatcher dispatcher = request.getRequestDispatcher("medico/cadastrar_paciente.jsp");
	    dispatcher.forward(request, response);
            
        } catch (SQLException ex) {
            Logger.getLogger(CadastrarPacienteServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
             
	}
       

}
